#!/bin/bash
DBNAME="oj_db"
DBUSER="root"
DBPASS="12345678"
DBPATH="./imau-oj/db/1.0.1.sql"

CPU=`cat /proc/cpuinfo| grep "processor"| wc -l`

yum -y update
yum -y install epel-release yum-utils
yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum -y install gcc-c++  mysql-devel glibc-static libstdc++-static flex java-1.8.0-openjdk java-1.8.0-openjdk-devel
yum -y install mariadb mariadb-devel mariadb-server
yum -y install policycoreutils-python git

systemctl start mariadb.service
useradd -m -u 1536 judge
cd /home/judge/
mkdir etc data log backup src
git clone https://gitee.com/wuma/imau-oj.git
mysql -h localhost -u${DBUSER} < ${DBPATH}
cp ./imau-oj/install/other/* ./etc/

if grep "OJ_SHM_RUN=0" ./etc/judge.conf ; then
	mkdir run0 run1 run2 run3
fi

sed -i "s/OJ_COMPILE_CHROOT=1/OJ_COMPILE_CHROOT=0/g" ./etc/judge.conf
sed -i "s/OJ_RUNNING=1/OJ_RUNNING=$CPU/g" ./etc/judge.conf

chmod 700 ./backup
chmod 700 ./etc/judge.conf
chmod 775 -R ./data
chmod 755 /home/judge

systemctl enable mariadb.service

cd ./imau-oj/install/core/
chmod +x make.sh
bash ./make.sh

if grep "/usr/bin/judged" /etc/rc.local ; then
	echo "auto start judged added!"
else
	chmod +x /etc/rc.d/rc.local
	sed -i "s/exit 0//g" /etc/rc.d/rc.local
	echo "/usr/bin/judged" >> /etc/rc.d/rc.local
	echo "exit 0" >> /etc/rc.d/rc.local
	
fi
/usr/bin/judged

rm -rf /home/judge/imau-oj

echo "Install Judge Ok"












